package com.sxz.sbs.module.client;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.CompactListAdapter;
import com.sxz.sbs.module.client.bean.CompactBean;
import com.sxz.sbs.module.client.presenter.CompactListPresenter;
import com.sxz.sbs.module.client.view.CompactListView;
import com.sxz.sbs.widget.CommonTitle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 合同文件 和定金凭证列表
 * @author tian
 */
public class CompactListActivity extends BaseActivity<CompactListPresenter> implements CompactListView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    /**
     * 合同类型
     */
    private int tag,customId;
    private int currentPage = 1;
    private List<CompactBean.ListBean> dataList = new ArrayList<>();
    private CompactListAdapter adapter;
    @Override
    protected void init() {
        Intent intent = getIntent();
        tag = intent.getIntExtra("compact_type", 0);
        customId = intent.getIntExtra("customId",0);
        if (tag == 1) {
            new CommonTitle.Builder(this).setTitle("定金凭证列表").build().show();
        } else if (tag == 2) {
            new CommonTitle.Builder(this).setTitle("合同文件列表").build().show();
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getCompactList();
            }
        });
        refreshLayout.setRefreshing(true);
        getCompactList();
        adapter = new CompactListAdapter(dataList);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(CompactListActivity.this,EvidenceDetailActivity.class)
                .putExtra("evidence_type",tag)
                .putExtra("evidence_id",dataList.get(position).getAchieveId()));
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getCompactList();
            }
        },recyclerView);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getCompactList();
            }
        });

    }
    private void getCompactList () {
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("customId", customId);
        map.put("valStatus", tag);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getCompactList(map);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_compact_list;
    }

    @Override
    protected CompactListPresenter createPresenter() {
        return new CompactListPresenter();
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        adapter.loadMoreFail();
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (currentPage == 1) {
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }
        } else {
            adapter.loadMoreFail();
        }
    }

    @Override
    public void onGetCompactListSuccess(CompactBean data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.getList() != null && data.getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(data.getList());
                    adapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(data.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    adapter.loadMoreEnd();
                }
            }

        }
    }
}
